package google.entry.update.updateTaskUrl;

import db.dao.BaseDao;
import domain.task.Task8;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2017/1/29.
 */
public class Update8 extends Thread {

    public static void main(String[] args) {
        new Update8().start();
    }

    @Override
    public void run() {
        BaseDao baseDao = new BaseDao();
        List<Task8> task8s = baseDao.find("from Task8");
        //[HOST]AAR&safe=strict&tbs=cdr:1,cd_min:2004/2/15,cd_max:2004/2/21&tbm=nws
        Pattern pattern = Pattern.compile("cd_min:(.+?),cd_max:(.+?)&");
        List<Task8> list = new ArrayList<>();
        for (Task8 task8 : task8s) {
            String url = task8.getUrl();
            Matcher matcher = pattern.matcher(url);
            if (matcher.find()) {
                String period = task8.getPeriod();
                String[] periodArr = period.split("/");
                String[] minArr = periodArr[0].split("-");
                String[] maxArr = periodArr[1].split("-");
                String cdMin = minArr[1] + "/" + minArr[2] + "/" + minArr[0];
                String cdMax = maxArr[1] + "/" + maxArr[2] + "/" + maxArr[0];
                url = url.replace(matcher.group(1), cdMin).replace(matcher.group(2), cdMax).replaceAll(":", "%3A").replaceAll(",", "%2C").replaceAll("/", "%2F");
                task8.setUrl(url);
                list.add(task8);
            } else {
                System.exit(-1);
            }
        }
        baseDao.update(list, list.size());
    }
}
